package cn.com.yusys.yusp.custManager.service;

import cn.com.yusys.yusp.custManager.domain.OcrmFCiFqScol;
import cn.com.yusys.yusp.custManager.domain.OcrmFCiFqSsolution;
import cn.com.yusys.yusp.custManager.repository.mapper.FlexibleQueryConMapper;
import cn.com.yusys.yusp.custManager.repository.mapper.FlexibleQuerySolutionMapper;
import cn.com.yusys.yusp.commons.mapper.CommonMapper;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.security.SecurityUtils;
import cn.com.yusys.yusp.commons.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: zoie
 * @Description: 灵活查询方案
 * @Date: 2021/5/1
 * @Version: 1.0
 */
@Service
public class FlexibleQuerySolutionService extends CommonService {

    @Autowired
    private FlexibleQuerySolutionMapper flexibleQuerySolutionMapper;

    @Autowired
    private FlexibleQueryConMapper colMapper;

    /**
     * UUID生成器
     * @return uuid
     */
    private String getUUID() {
        return UUID.randomUUID().toString().toLowerCase().replace("-", "");
    }

    @Override
    protected CommonMapper getMapper() {
        return this.flexibleQuerySolutionMapper;
    }

    /**
     * 查询方案列表
     *
     * @param queryModel
     * @return list
     */
    public List<Map<String, Object>> getList(QueryModel queryModel) {
        String loginCode = SecurityUtils.getCurrentUserLogin();
        // 非管理员用户，只能查询自己创建的方案
        if (!"admin".equals(loginCode)) {
            queryModel.getCondition().put("loginCode", loginCode);
        }
        return flexibleQuerySolutionMapper.getList(queryModel);
    }

    /**
     * 新增方案
     *
     * @param scolList
     * @param solutionList
     * @return int
     * @throws
     */
    @Transactional(readOnly = false, rollbackFor = { Exception.class, RuntimeException.class })
    public int saveSolution(List<OcrmFCiFqScol> scolList, List<OcrmFCiFqSsolution> solutionList) {
        int count = 0;
        // 用户信息
        //String orgCode = dto.getBody().getOrg().getCode();
        String loginCode = SecurityUtils.getCurrentUserLogin();
        //String instuCode = dto.getBody().getInstu().getCode();
        // 方案信息
        OcrmFCiFqSsolution solution = solutionList.get(0);
        OcrmFCiFqSsolution newSolution = new OcrmFCiFqSsolution();
        newSolution.setId(this.getUUID());
        newSolution.setSsType(solution.getSsType());
        newSolution.setSsSort(solution.getSsSort());
        newSolution.setSsResult(solution.getSsResult());
        newSolution.setSsName(solution.getSsName());
        newSolution.setTopNum(solution.getTopNum());
        newSolution.setSsUser(loginCode);
        //newSolution.setCreateOrg(orgCode);
        //newSolution.setCorpOrgCode(instuCode);
        newSolution.setSsRelReport("0");
        newSolution.setSsDate(new Date());
        flexibleQuerySolutionMapper.insertSelective(newSolution);
        // 显示列信息
        for (int i = 0; i < scolList.size(); i++) {
            scolList.get(i).setId(this.getUUID());
            scolList.get(i).setSsId(newSolution.getId());
            colMapper.insertSelective(scolList.get(i));
        }
        return count;
    }

    /**
     * 批量删除方案
     * @param ids
     * @return int
     */
    @Transactional(readOnly = false, rollbackFor = { Exception.class, RuntimeException.class })
    public int batchDeleteSolution(String ids) {
        String[] idList = ids.toString().split(",");
        for (int i = 0; i < idList.length; i++) {
            flexibleQuerySolutionMapper.deleteByssId(idList[i]);
            colMapper.deletebyssid(idList[i]);
        }
        return idList.length;
    }
}
